<template>
  <el-dialog
    :title="!dataForm.mapNo ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="地图名称" prop="mapName">
      <el-input v-model="dataForm.mapName" placeholder="地图名称"></el-input>
    </el-form-item>
<!--    <el-form-item label="地图的本地保存路径" prop="mapLocalpath">-->
<!--      <el-input v-model="dataForm.mapLocalpath" placeholder="地图的本地保存路径"></el-input>-->
<!--    </el-form-item>-->
    <el-form-item label="是否正在载入使用" prop="mapIsload">
      <el-input v-model="dataForm.mapIsload" placeholder="是否正在载入使用"></el-input>
    </el-form-item>
    <el-form-item label="地图矩形的左上角坐标X" prop="mapLefttopx">
      <el-input v-model="dataForm.mapLefttopx" placeholder="地图矩形的左上角坐标X"></el-input>
    </el-form-item>
    <el-form-item label="地图矩形的左上角坐标Y" prop="mapLefttopy">
      <el-input v-model="dataForm.mapLefttopy" placeholder="地图矩形的左上角坐标Y"></el-input>
    </el-form-item>
    <el-form-item label="地图矩形的左上角坐标Z" prop="mapLefttopz">
      <el-input v-model="dataForm.mapLefttopz" placeholder="地图矩形的左上角坐标Z"></el-input>
    </el-form-item>
    <el-form-item label="地图矩形的右下角坐标X" prop="mapRightbottomx">
      <el-input v-model="dataForm.mapRightbottomx" placeholder="地图矩形的右下角坐标X"></el-input>
    </el-form-item>
    <el-form-item label="地图矩形的右下角坐标Y" prop="mapRightbottomy">
      <el-input v-model="dataForm.mapRightbottomy" placeholder="地图矩形的右下角坐标Y"></el-input>
    </el-form-item>
    <el-form-item label="地图矩形的右下角坐标Z" prop="mapRightbottomz">
      <el-input v-model="dataForm.mapRightbottomz" placeholder="地图矩形的右下角坐标Z"></el-input>
    </el-form-item>
    <el-form-item label="地图描述" prop="mapDescription">
      <el-input v-model="dataForm.mapDescription" placeholder="地图描述"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          mapNo: 0,
          mapName: '',
          mapIsload: '',
          mapLefttopx: '',
          mapLefttopy: '',
          mapLefttopz: '',
          mapRightbottomx: '',
          mapRightbottomy: '',
          mapRightbottomz: '',
          mapDescription: ''
        },
        dataRule: {
          mapName: [
            { required: true, message: '地图名称不能为空', trigger: 'blur' }
          ],
          // mapLocalpath: [
          //   { required: true, message: '地图的本地保存路径不能为空', trigger: 'blur' }
          // ],
          mapIsload: [
            { required: true, message: '是否正在载入使用不能为空', trigger: 'blur' }
          ],
          mapLefttopx: [
            { required: true, message: '地图矩形的左上角坐标X不能为空', trigger: 'blur' }
          ],
          mapLefttopy: [
            { required: true, message: '地图矩形的左上角坐标Y不能为空', trigger: 'blur' }
          ],
          mapLefttopz: [
            { required: true, message: '地图矩形的左上角坐标Z不能为空', trigger: 'blur' }
          ],
          mapRightbottomx: [
            { required: true, message: '地图矩形的右下角坐标X不能为空', trigger: 'blur' }
          ],
          mapRightbottomy: [
            { required: true, message: '地图矩形的右下角坐标Y不能为空', trigger: 'blur' }
          ],
          mapRightbottomz: [
            { required: true, message: '地图矩形的右下角坐标Z不能为空', trigger: 'blur' }
          ],
          mapDescription: [
            { required: true, message: '地图描述不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.mapNo = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.mapNo) {
            this.$http({
              url: this.$http.adornUrl(`/map/maptable/info/${this.dataForm.mapNo}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.mapName = data.maptable.mapName
                // this.dataForm.mapLocalpath = data.maptable.mapLocalpath
                this.dataForm.mapIsload = data.maptable.mapIsload
                this.dataForm.mapLefttopx = data.maptable.mapLefttopx
                this.dataForm.mapLefttopy = data.maptable.mapLefttopy
                this.dataForm.mapLefttopz = data.maptable.mapLefttopz
                this.dataForm.mapRightbottomx = data.maptable.mapRightbottomx
                this.dataForm.mapRightbottomy = data.maptable.mapRightbottomy
                this.dataForm.mapRightbottomz = data.maptable.mapRightbottomz
                this.dataForm.mapDescription = data.maptable.mapDescription
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/map/maptable/${!this.dataForm.mapNo ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'mapNo': this.dataForm.mapNo || undefined,
                'mapName': this.dataForm.mapName,
                // 'mapLocalpath': this.dataForm.mapLocalpath,
                'mapIsload': this.dataForm.mapIsload,
                'mapLefttopx': this.dataForm.mapLefttopx,
                'mapLefttopy': this.dataForm.mapLefttopy,
                'mapLefttopz': this.dataForm.mapLefttopz,
                'mapRightbottomx': this.dataForm.mapRightbottomx,
                'mapRightbottomy': this.dataForm.mapRightbottomy,
                'mapRightbottomz': this.dataForm.mapRightbottomz,
                'mapDescription': this.dataForm.mapDescription
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
